#!/bin/bash
###SHELLPACK preamble mpas-bench v6.2
MIRROR_LOCATION="$WEBROOT/mpas"

###SHELLPACK parseargBegin
###SHELLPACK parseargParam --model	MPAS_MODEL
###SHELLPACK parseargParam --processes  MPAS_PROCESSES
###SHELLPACK parseargParam --threads    MPAS_THREADS
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

install-depends curl libcurl-devel

export HDF5_VERSION="hdf5-1_10_5"
export NETCDFC_VERSION="v4.7.3"
export NETCDFFC_VERSION="v4.5.2"
export PNETCDF_VERSION="1.12.1"
export PIO_VERSION="pio_2_5_1"

MPIRUN=
if [ "$MPAS_OPENMPI_VERSION" != "" ]; then
	###SHELLPACK mpi_setup_deps MPAS_OPENMPI_VERSION
	###SHELLPACK mpi_setup_env MPAS_OPENMPI_VERSION MPAS_MPI_PATH MPAS_MPI_LIBPATH MPAS_MPI_OPTS
	###SHELLPACK mpi_setup_shmem
	MPIRUN="mpirun $MPAS_MPI_OPTS $MMTESTS_MPI_OPTS -np $MPAS_PROCESSES"
	export OMP_NUM_THREADS=$MPAS_THREADS
fi

###SHELLPACK check_external_install_required pnetcdfbuild   pnetcdfbuild-$PNETCDF_VERSION   $PNETCDF_VERSION
###SHELLPACK check_external_install_required hdf5build      hdf5build-$HDF5_VERSION         $HDF5_VERSION
###SHELLPACK check_external_install_required netcdfcbuild   netcdfcbuild-$NETCDFC_VERSION   $NETCDFC_VERSION
###SHELLPACK check_external_install_required netcdffcbuild  netcdffcbuild-$NETCDFFC_VERSION $NETCDFFC_VERSION
###SHELLPACK check_external_install_required piobuild       piobuild-$PIO_VERSION           $PIO_VERSION

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$SHELLPACK_SOURCES/mpasdeps-installed/$MMTESTS_LIBDIR
export PATH=$SHELLPACK_SOURCES/mpasdeps-installed/bin:$PATH

###SHELLPACK check_install_required_continue mpas-$VERSION

if [ "$INSTALL_ONLY" = "yes" -o ! -d model-$MPAS_MODEL ]; then
	case $MPAS_MODEL in
	jwbaroclinic)
		file_fetch http://www2.mmm.ucar.edu/projects/mpas/test_cases/v7.0/jw_baroclinic_wave.tar.gz \
			$MIRROR_LOCATION/jw_baroclinic_wave.tar.gz \
			$SHELLPACK_SOURCES/jw_baroclinic_wave.tar.gz

		tar xf $SHELLPACK_SOURCES/jw_baroclinic_wave.tar.gz
		mv jw_baroclinic_wave model-$MPAS_MODEL
		;;
	supercell)
		file_fetch http://www2.mmm.ucar.edu/projects/mpas/test_cases/v7.0/supercell.tar.gz \
			$MIRROR_LOCATION/supercell.tar.gz \
			$SHELLPACK_SOURCES/supercell.tar.gz

		tar xf $SHELLPACK_SOURCES/supercell.tar.gz
		mv supercell model-$MPAS_MODEL
		;;
	*)
		die "Unrecognised model $MPAS_MODEL"
	esac

	# Remove lbc lines in all input files
	sed -i '/immutable_stream name="lbc"/,/output_interval="none"/d'   model-$MPAS_MODEL/streams.init_atmosphere
	sed -i '/immutable_stream name="lbc_in"/,/input_interval="none"/d' model-$MPAS_MODEL/streams.atmosphere
fi

PREPARE_CMD="$MPIRUN ./init_atmosphere_model"
mpas_prepare() {
	echo Preparation: $PREPARE_CMD
	save_rc $TIME_CMD -o init_atmosphere_model-time.$MPAS_MODEL \
		$PREPARE_CMD 2>&1 | tee init_atmosphere_model-log.$MPAS_MODEL
	recover_rc
	if [ $? -ne 0 ]; then
		die "Failed to execute $PREPARE_CMD"
	fi
}

# Prepare model directory for execution
cd $SHELLPACK_SOURCES/mpas-$VERSION-installed/model-$MPAS_MODEL ||
	die "Failed to cd to model-$MPAS_MODEL"
for BIN in init_atmosphere_model atmosphere_model; do
	if [ ! -e $BIN ]; then
		ln -s ../$BIN $BIN || die "Failed to symbolic link $BIN"
	fi
done

if [ "$INSTALL_ONLY" = "yes" -a "$MPAS_PREPARE" = "yes" ]; then
	mpas_prepare
fi

###SHELLPACK init_complete

BENCH_CMD="$MPIRUN ./atmosphere_model"
log_runtime "OMP_NUM_THREADS=$MPAS_THREADS"
log_runtime "OMP_PLACES=$OMP_PLACES"
log_runtime "OMP_PROC_BIND=$OMP_PROC_BIND"
log_cmd "$BENCH_CMD"

log_runtime "Run binaries"
for BIN in atmosphere_model init_atmosphere_model build_tables; do
	log_linkages `find $SHELLPACK_SOURCES/mpas-$VERSION-installed/ -name $BIN`
done

# Prepare if it was not done at installation time
if [ "$MPAS_PREPARE" != "yes" ]; then
	monitor_pre_hook $LOGDIR_RESULTS $MPAS_MODEL-prepare
	mpas_prepare
	monitor_post_hook $LOGDIR_RESULTS $MPAS_MODEL-prepare
	mv init_atmosphere_model-time.$MPAS_MODEL $LOGDIR_RESULTS/
fi
mv init_atmosphere_model-log.$MPAS_MODEL $LOGDIR_RESULTS/ 2> /dev/null

# Preserve prepare stdout and stderr
tar -czf $LOGDIR_RESULTS/logs-prepare.tar.gz log.init_atmosphere.0*

# Benchmark
monitor_pre_hook $LOGDIR_RESULTS $MPAS_MODEL-bench
echo Benchmark: $BENCH_CMD
save_rc $TIME_CMD -o $LOGDIR_RESULTS/atmosphere_model-time.$MPAS_MODEL \
	$BENCH_CMD 2>&1 | tee $LOGDIR_RESULTS/atmosphere_model-log.$MPAS_MODEL
recover_rc
if [ $? -ne 0 ]; then
	die "Failed to execute $BENCH_CMD"
fi
monitor_post_hook $LOGDIR_RESULTS $MPAS_MODEL-bench

gzip $LOGDIR_RESULTS/init_atmosphere_model-log.$MPAS_MODEL 2> /dev/null
gzip $LOGDIR_RESULTS/atmosphere_model-log.$MPAS_MODEL
tar -czf $LOGDIR_RESULTS/logs-bench.tar.gz log.atmosphere.0*

exit $SHELLPACK_SUCCESS
